package com.mapreduce;

import com.bean.YearTemp;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.net.URI;

public class SecondSortMapReduce extends Configured
        implements Tool {
    static class SecondSortMapper extends
            Mapper<LongWritable, Text, YearTemp, IntWritable>{
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, YearTemp, IntWritable>.Context context) throws IOException, InterruptedException {
//            value:1996,25
            String[] datas = value.toString().split(",");
            // datas:["1996","25"]
            YearTemp yearTemp =
                    new YearTemp(
                            datas[0],Integer.parseInt(datas[1]));
            context.write(yearTemp,
                    new IntWritable(Integer.parseInt(datas[1])));

            //k2						v2
//YearTemp(1996,25)			25
//YearTemp(1996,30)			30
//YearTemp(1995,33)			33
        }

    }
    static class SecondSortReducer extends
            Reducer<YearTemp, IntWritable, Text, IntWritable>{
        @Override
        protected void reduce(YearTemp key, Iterable<IntWritable> values, Reducer<YearTemp, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            for (IntWritable val:values){
                // 把年份取出来
                context.write(new Text(key.getYear()),val);
            }
        }
    }



    @Override
    public int run(String[] args) throws Exception {
        Configuration conf = getConf();
        // 指定输入输出路径
        Path input = new Path(
                "hdfs://192.168.10.11:9000/sortdata");
        Path output = new Path(
                "hdfs://192.168.10.11:9000/soutput");

        FileSystem fs = FileSystem.get(
                new URI("hdfs://192.168.10.11:9000")
                ,conf);
        if (fs.exists(output)) fs.delete(output,true);
        //构建Job
        Job job = Job.getInstance(conf);
        job.setJobName("second");
        job.setJarByClass(this.getClass());

        job.setMapperClass(SecondSortMapper.class);
        job.setMapOutputKeyClass(YearTemp.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setReducerClass(SecondSortReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setNumReduceTasks(2);
        // 设置分区器
        job.setPartitionerClass(YearPar.class);
        // 设置分组规则
        job.setGroupingComparatorClass(YearGroup.class);

        TextInputFormat.addInputPath(job,input);
        TextOutputFormat.setOutputPath(job,output);
        return job.waitForCompletion(true)?0:-1;
    }

    public static void main(String[] args) throws Exception {
        System.exit(ToolRunner.run(new SecondSortMapReduce(),args));
    }
}
